function toggle_filter (filter_id) {

	/*
	 *
	 * Default state of filter is visible.
	 * if it is hidden then then hidden will change the 
	 * visibility attribute to visible (default is undefined
	 * so don't check on first attempt).
	 *
	 */ 

	if ($('#'+filter_id).attr ('visibility') == 'visible') {
		/* hide the filter on click and set
		 * the visibility attribute to hidden */
		$('#'+filter_id).hide ('slow');
		$('#'+filter_id).attr('visibility', 'hidden');
	} else {
		/* show the filter on click and set
		 * the visibility attribute to visibility */
		$('#'+filter_id).show ('slow');
		$('#'+filter_id).attr('visibility', 'visible');
	}
}
function toggle_fieldset (filter_id) {

	/*
	 *
	 * Default state of filter is visible.
	 * if it is hidden then the hidden will change the 
	 * visibility attribute to visible (default is undefined
	 * so don't check on first attempt).
	 *
	 */

	if ($('#'+filter_id).attr ('visibility') == 'hidden') {
		/* hide the filter on click and set
		 * the visibility attribute to hidden */
		$('#'+filter_id).show ('slow');
		$('#'+filter_id).attr('visibility', 'visible');
	} else {
		/* show the filter on click and set
		 * the visibility attribute to visibility */
		$('#'+filter_id).hide ('slow');
		$('#'+filter_id).attr('visibility', 'hidden');
	}
}
function clone_filter (filter_id) {

	/*
	 *
	 * clone filter by filter id.
	 * unset the id attribute on clone.
	 * unset the "td > input" field value in clone.
	 * wrap the search label with delete div to remove filter option
	 * insert before the filter control options
	 *
	 */
	var new_filter = $('#'+filter_id).clone (true);
	new_filter.attr ('id', '');
	$('td > input', new_filter).attr('value', '');
	//try to reset the selection on the new filter
	//$('td > select', new_filter).attr('selected', "false");
	$('td > div', new_filter).wrap ('<div class="css_icon_filter_delete" onclick="javascript:delete_filter (this);"></div>');
	new_filter.insertBefore ($('#'+filter_id+'_ctl'));
}
function delete_filter (filter) {

	$(filter).parent ().parent ().remove ();
}

function get_cookie (name) {

	var cookies = document.cookie.split ('; ');
	for (a in cookies) {

		var cookie = cookies[a].split ('=');
		if (cookie[0] == name) {
			return cookie[1];
		}
	}
}
function set_cookie (name, value) {

	var cookies = document.cookie.split ('; ');
	var set_cookies = new Array ();
	var cookie_set = false;

	for (a in cookies) {

		var cookie = cookies[a].split ('=');
		if (cookie[0] == name) {
			cookie[1] = value;
			cookie_set = true;
		}
		document.cookie = cookie.join ('=');
	}

	if (!cookie_set) {

		var cookie = new Array (name, value); 
		document.cookie = cookie.join ('=');
	}

	document.cookie = set_cookies.join (';');
}

function list_checkbox_toggle (checkbox) {
	checkbox.checked ? list_checkbox_select (checkbox) : list_checkbox_unselect (checkbox);
}

function list_checkbox_select (checkbox) {

	var value = checkbox.value;
	var list_object = checkbox.getAttribute ('list_object');
	var check_list = get_cookie (list_object);
	var checked_list = (check_list ? check_list.split (':') : new Array ());
	var found = false;

	for (a in checked_list) {

		if (a == value) found = true;
	}
	
	if (!found) checked_list.push (value);

	set_cookie (list_object, checked_list.join (':'));
}

function list_checkbox_unselect (checkbox) {

	var value = checkbox.value;
	var list_object = checkbox.getAttribute ('list_object');
	var check_list = get_cookie (list_object);
	var checked_list = check_list ? check_list.split (':') : new Array ();
	var list_checked = new Array ();

	for (a in checked_list) {

		if (value != a) list_checked.push (a);
	}

	set_cookie (list_object, list_checked.join (':'));
}
